﻿using CasignDesignGA.Casign;
using CasignDesignGA.Casing;
using Microsoft.VisualBasic.FileIO;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CasignDesignGA.Engine.Population.FileReader
{
    class CSVFileReader : IFileReader
    {
        public List<CasingUnit> readCasingCatalogFile(string fileName)
        {
            List<CasingUnit> csvList = new List<CasingUnit>();
            try
            {
                using (TextFieldParser csvReader = new TextFieldParser(fileName))
                {
                    csvReader.SetDelimiters(new string[] { ";" });
                    csvReader.HasFieldsEnclosedInQuotes = true;
                    //read column names
                    string[] colFields = csvReader.ReadFields();
                    foreach (string column in colFields)
                    {
                        DataColumn datecolumn = new DataColumn(column);
                        datecolumn.AllowDBNull = true;
                    }
                    while (!csvReader.EndOfData)
                    {
                        string[] fieldData = csvReader.ReadFields();
                        //Making empty value as null
                        CasingUnit aCasing = new CasingUnit();
                        aCasing.Od = Double.Parse(fieldData[0]);
                        aCasing.MetricWeight = Double.Parse(fieldData[1]);
                        aCasing.SteelGrade = fieldData[2];
                        aCasing.Id = Double.Parse(fieldData[3]);
                        aCasing.Ys = Double.Parse(fieldData[4]);
                        aCasing.Drift = Double.Parse(fieldData[5]);
                        aCasing.Burst = Double.Parse(fieldData[7]);
                        aCasing.Collapse = Double.Parse(fieldData[8]);
                        aCasing.Axial = Double.Parse(fieldData[9]);
                        aCasing.Uts = Double.Parse(fieldData[10]);
                        aCasing.WallThickness = Double.Parse(fieldData[11]);
                        aCasing.Price = Double.Parse(fieldData[12]);

                        csvList.Add(aCasing);

                    }
                }
            }
            catch (Exception ex)
            {
                ex.ToString();
            }
            return csvList;
        }

        public void readCasingPropertiesFile(string fileName, string propertySelected)
        {
            try
            {
                using (TextFieldParser csvReader = new TextFieldParser(fileName))
                {
                    CasingProperty aProperty = new CasingProperty();
                    csvReader.SetDelimiters(new string[] { ";" });
                    csvReader.HasFieldsEnclosedInQuotes = true;
                    //read column names
                    string[] colFields = csvReader.ReadFields();
                    foreach (string column in colFields)
                    {
                        DataColumn datecolumn = new DataColumn(column);
                        datecolumn.AllowDBNull = true;
                    }
                    while (!csvReader.EndOfData)
                    {
                        string[] fieldData = csvReader.ReadFields();
                        //Making empty value as null
                        double fieldDouble = Convert.ToDouble(fieldData[0]);
                        if (!aProperty.contains(fieldDouble))
                        {
                            double valueDouble = Convert.ToDouble(fieldData[1]);
                            aProperty.Add(fieldDouble, valueDouble);
                        }
                    }
                    CasingProperties.Instance.AddProperty(propertySelected, aProperty);
                }
            }
            catch (Exception ex)
            {
                ex.ToString();
            }
            
        }
    }
}
